package com.snap.vseries.dialog;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.snap.vseries.AppConfig;
import com.snap.vseries.BuildConfig;
import com.snap.vseries.R;
import com.snap.vseries.bean.ExpInfo;
import com.snap.vseries.fragment.InnerCTFragment;
import com.snap.vseries.fragment.InnerCrossFragment;
import com.snap.vseries.fragment.InnerParamFragment;
import com.snap.vseries.ui.widget.TextButton;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

public class AnalysisDialog extends DialogFragment {

    protected Unbinder unbinder;
    @BindView(R.id.radio_group)
    RadioGroup segment;
    @BindView(R.id.radio3)
    RadioButton paramRb;
    @BindView(R.id.sure_btn)
    TextButton sureBtn;
    @BindView(R.id.cancel_btn)
    TextButton cancelBtn;

    private InnerCTFragment ctFragment;
    private InnerCrossFragment crossFragment;
    private InnerParamFragment paramFragment;

    private ExpInfo experimentInfo;
    private AnalysisChangeListener listener;
    private int currentCheckedId = 0;

    public AnalysisDialog setParam(ExpInfo experimentInfo, AnalysisChangeListener listener) {
        this.experimentInfo = experimentInfo;
        this.listener = listener;
        return this;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(STYLE_NO_TITLE, R.style.DimDialogStyle);
    }

    @Override
    public void onStart() {
        super.onStart();
        getDialog().setCanceledOnTouchOutside(true);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.dialog_analysis, container, false);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onDestroyView() {
        if (unbinder != null) unbinder.unbind();
        super.onDestroyView();
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        paramRb.setVisibility(BuildConfig.DEBUG || AppConfig.Dynamic.analysis_debug_mode ? View.VISIBLE : View.GONE);
        sureBtn.setOnClickListener(v -> {
            dismiss();
            apply();
        });
        cancelBtn.setOnClickListener(v -> dismiss());
        segment.check(R.id.radio1);
        showFragment(R.id.radio1);
        segment.setOnCheckedChangeListener((group, checkedId) -> showFragment(checkedId));
    }

    private void apply() {
        if (ctFragment != null) ctFragment.apply();
        if (crossFragment != null) crossFragment.apply();
        if (paramFragment != null) paramFragment.apply();
        if (listener != null) listener.onAnalysisChanged();
    }

    private void showFragment(int checkedId) {
        if (currentCheckedId == checkedId) {
            return;
        }
        currentCheckedId = checkedId;

        FragmentManager fm = getChildFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();

        ctFragment = (InnerCTFragment) fm.findFragmentByTag(InnerCTFragment.class.getSimpleName());
        crossFragment = (InnerCrossFragment) fm.findFragmentByTag(InnerCrossFragment.class.getSimpleName());
        paramFragment = (InnerParamFragment) fm.findFragmentByTag(InnerParamFragment.class.getSimpleName());
        hideAll(ft);

        switch (currentCheckedId) {
            case R.id.radio1:
                if (ctFragment == null) {
                    ctFragment = new InnerCTFragment(experimentInfo);
                    ft.add(R.id.container, ctFragment, InnerCTFragment.class.getSimpleName());
                } else {
                    ft.show(ctFragment);
                }
                break;
            case R.id.radio2:
                if (crossFragment == null) {
                    crossFragment = new InnerCrossFragment(experimentInfo);
                    ft.add(R.id.container, crossFragment, InnerCrossFragment.class.getSimpleName());
                } else {
                    ft.show(crossFragment);
                }
                break;
            case R.id.radio3:
                if (paramFragment == null) {
                    paramFragment = new InnerParamFragment(experimentInfo);
                    ft.add(R.id.container, paramFragment, InnerParamFragment.class.getSimpleName());
                } else {
                    ft.show(paramFragment);
                }
                break;
            default:
        }
        ft.commitAllowingStateLoss();
    }

    private void hideAll(FragmentTransaction ft) {
        if (ctFragment != null) {
            ft.hide(ctFragment);
        }
        if (crossFragment != null) {
            ft.hide(crossFragment);
        }
        if (paramFragment != null) {
            ft.hide(paramFragment);
        }
    }

    public interface AnalysisChangeListener {
        void onAnalysisChanged();
    }
}
